﻿using AdminBase.Common.Helpers;
using AdminBase.Model;
using AdminBase.Model.Common;
using AdminBase.Model.Entitys;
using AdminBase.Common.Attributes;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using AdminBase.Interface;

namespace AdminBase.webapi.Controllers
{
    /// <summary>
    /// 角色控制器
    /// </summary>
    [ApiController]
    [Authorize]
    [Route("[controller]")]
    [ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ApiVersions.V1))]
    [TypeFilter(typeof(AsyncExceptionFilterAttribute))]
    public class RolesController : ControllerBase
    {
        // 业务接口
        private readonly IRolesService _role;
        /// <summary>
        /// 构造注入
        /// </summary>
        /// <param name="role"></param>
        public RolesController(IRolesService role)
        {
            _role = role;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="role">实体</param>
        /// <returns></returns>
        [HttpPost]
        public ApiResult Add(Roles role)
        {
            //获取当前登录人信息 
            role.CreateUserName = HttpContext.User.Claims.FirstOrDefault(c => c.Type == "FullName")?.Value;
            role.UpdateUserName = HttpContext.User.Claims.FirstOrDefault(c => c.Type == "FullName")?.Value;
            return ResultHelper.Success(_role.Add(role));
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        [HttpPut]
        public ApiResult Edit(Roles role)
        {
            return ResultHelper.Success(_role.Edit(role));
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        [Route("{id}")]
        public ApiResult Del(long id)
        {
            return ResultHelper.Success(_role.Del(id));
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpDelete]
        public ApiResult BatchDel([FromBody] List<long> ids)
        {
            //获取当前登录人信息 
            return ResultHelper.Success(_role.BatchDel(ids));
        }

        /// <summary>
        /// 根据ID获取角色
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("{id:long}")]
        public ApiResult GetRole(long id)
        {
            return ResultHelper.Success(_role.GetRoleById(id));
        }

        /// <summary>
        /// 获取角色列表
        /// </summary>
        /// <param name="queryParam">查询参数</param>
        /// <param name="pageBase">分页查询</param>
        /// <returns></returns>
        [HttpGet]
        public ApiResult GetRoles([FromQuery] RolesQueryParam queryParam, [FromQuery] PageBase pageBase)
        {
            return ResultHelper.Success(_role.GetRoles(queryParam, pageBase));
        }

        /// <summary>
        /// 获取全部角色用于下拉框
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("option")]
        public ApiResult GetRolesOption()
        {
            return ResultHelper.Success(_role.GetRolesOption());
        }

        /// <summary>
        /// 设置权限
        /// </summary>
        /// <param name="rolesDto">角色ID权限IDs</param>
        /// <returns></returns>
        [HttpPatch]
        public ApiResult SetPermissions(RolesDto rolesDto)
        {
            return ResultHelper.Success(_role.SetPermissions(rolesDto));
        }
    }
}
